import os
import subprocess

#Set input and output folder pahts
input_folder = 'C:/Users/tfree/OneDrive/Desktop/danielle_wheel_running_photometry/input_folder/'
output_folder = 'C:/Users/tfree/OneDrive/Desktop/danielle_wheel_running_photometry/output_folder/'
udf_path = 'C:/Users/tfree/OneDrive/Desktop/danielle_wheel_running_photometry'

#Collect list of file names for processing
input_files = []
for file_name in os.listdir(input_folder):
	input_files.append(file_name)
	
#Main processing loop
for file_num, file_name in enumerate(input_files):
	output_folder_check = []
	for item in os.listdir(output_folder):
		output_folder_check.append(item)
	input_handle = input_folder + file_name
	if output_folder_check == []:
		os.mkdir(output_folder + file_name[:-4] +'/')
		os.mkdir(output_folder + file_name[:-4] +'/Event_Traces/')
		os.mkdir(output_folder + file_name[:-4] +'/Event_Traces/DFF0_Traces/')
		os.mkdir(output_folder + file_name[:-4] +'/Event_Traces/Zscore_Traces/')
		os.mkdir(output_folder + file_name[:-4] +'/Event_Traces/4s_Peri-Event_DFF0_Traces/')
		os.mkdir(output_folder + file_name[:-4] +'/Event_Traces/4s_Peri-Event_Zscore_Traces/')		
	if output_folder_check != [] and file_name[:-4] not in output_folder_check[0]:
		os.mkdir(output_folder + file_name[:-4] +'/')
		os.mkdir(output_folder + file_name[:-4] +'/Event_Traces/')
		os.mkdir(output_folder + file_name[:-4] +'/Event_Traces/DFF0_Traces/')
		os.mkdir(output_folder + file_name[:-4] +'/Event_Traces/Zscore_Traces/')
		os.mkdir(output_folder + file_name[:-4] +'/Event_Traces/4s_Peri-Event_DFF0_Traces/')
		os.mkdir(output_folder + file_name[:-4] +'/Event_Traces/4s_Peri-Event_Zscore_Traces/')		
	output_handle = output_folder + file_name[:-4] + '/' + file_name
	dff0_traceout_handle = output_folder + file_name[:-4] + '/Event_Traces/DFF0_Traces/' + file_name
	z_traceout_handle = output_folder + file_name[:-4] + '/Event_Traces/Zscore_Traces/' + file_name
	peri_4s_dff0_handle = output_folder + file_name[:-4] + '/Event_Traces/4s_Peri-Event_DFF0_Traces/' + file_name
	peri_4s_z_handle = output_folder + file_name[:-4] + '/Event_Traces/4s_Peri-Event_Zscore_Traces/' + file_name
	print('Processing: ' + file_name + ' (' + str(file_num + 1)+ ' of ' +str(len(input_files))+ ')...')
	subprocess.call(['python', 'process_data_import_peri_4s.py', input_handle, output_handle, peri_4s_dff0_handle, udf_path])	
	subprocess.call(['python', 'process_data_z-score_peri_4s.py', input_handle, output_handle, udf_path, peri_4s_z_handle, 'trace_dict_4s_peri.pkl', 'dff0_dict_4s_peri.pkl', 'base_epoch_4s_peri.pkl'])
	subprocess.call(['python', 'process_data_calcs_peri_4s.py', input_handle, output_handle, udf_path, 'trace_dict_4s_peri.pkl', 'peak_dict.pkl', 'behavior_dict_sort.pkl'])
print('Done')
